// const httpUrl = 'https://scrm.huofutp.com/'  // 线上接口访问地址
// #ifdef H5
export let httpUrl = '/api'  // 接访问地址
// #endif
// #ifdef APP-PLUS
export let httpUrl = "http://103.26.77.115:10003"
// #endif
// export let httpUrl = 'http://192.168.1.226:9090/wxcp'  // 测试服

export function get(url, data) {
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'GET',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                if (res.data.code == 200) {
                    resolve(res.data)
                } else {
                    reject(res.data)
                    throwFuc(res.data)
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
    return promise
}

export function getMap(url) {
  let promise = new Promise((resolve, reject) => {
      uni.request({
          url: url,
          method: 'GET',
          dataType: 'json',
          header: {
              'content-type': 'application/json',
          },
          success: function (res) {
              if (res.data.code == 200) {
                  resolve(res.data)
              } else {
                  reject(res.data)
                  throwFuc(res.data)
              }
          },
          fail: (err) => {
              reject(err)
          }
      })
  })
  return promise
}

export function post(url, data) {
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'POST',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                if (res.data.code == 200) {
                    resolve(res.data)
                } else {
                    if(res.data.status == 404){
                        res.data.msg = '网络地址错误'
                    }
                    reject(res.data)
                    throwFuc(res.data)
                }
            },
            fail: (err) => {
                console.log(err, 336)
                reject(err)
            }
        })
    })
    return promise
}

const throwFuc = (data) => {
    let status = data.code
    let msg = data.msg
    let message = ""
    if (status == 500) {
        message = '网络错误(500)'
    } else {
        message = msg
    }
}